package cn.com.git.workflow.service.impl;

import cn.com.git.common.base.BaseServiceImpl;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import cn.com.git.common.util.object.MapperFacadeUtils;
import cn.com.git.workflow.dao.WorkFlowGroupDAO;
import cn.com.git.workflow.dao.model.WorkFlowGroup;
import cn.com.git.workflow.service.WorkFlowGroupService;
import cn.com.git.workflow.service.dto.WorkFlowGroupListDTO;
import cn.com.git.workflow.service.dto.WorkFlowGroupDetailDTO;
import cn.com.git.workflow.service.dto.WorkFlowGroupSaveDTO;
import cn.com.git.workflow.service.query.WorkFlowGroupListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* WorkFlowGroup ServiceImpl 接口
* @author zhangzhenfu
* @Description
* @date 2020/5/28
*/
@Transactional
@Service
public class WorkFlowGroupServiceImpl extends BaseServiceImpl implements WorkFlowGroupService {

    @Autowired
    private WorkFlowGroupDAO workFlowGroupDAO;

    /*=================================*/
    /*============ WorkFlowGroup =============*/
    /*=================================*/
    @Override
    public ResultPage<WorkFlowGroupListDTO> listWorkFlowGroup(WorkFlowGroupListQuery query) {
        List<WorkFlowGroupListDTO> data = new ArrayList<>();
        List<WorkFlowGroup> list = workFlowGroupDAO.selectList(query);
            Optional.ofNullable(list).orElse(Lists.newArrayList())
            .stream().forEach(item->{
                WorkFlowGroupListDTO d = new WorkFlowGroupListDTO();
                d.setM(MapperFacadeUtils.map(item,WorkFlowGroupListDTO.WorkFlowGroup.class));
                data.add(d);
            });
        return new ResultPage(ResultConstant.SUCCESS,data,data.size());
    }

    @Override
    public ResultMsg<WorkFlowGroupDetailDTO> findWorkFlowGroup(Integer id) {
        WorkFlowGroupDetailDTO d = new WorkFlowGroupDetailDTO();
        WorkFlowGroup workFlowGroup = workFlowGroupDAO.selectById(id);
        d.setM(MapperFacadeUtils.map(workFlowGroup,WorkFlowGroupDetailDTO.WorkFlowGroup.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,d);
    }

    @Override
    public ResultMsg<WorkFlowGroupSaveDTO> addWorkFlowGroup(WorkFlowGroupSaveDTO saveDTO) {
        Integer id = workFlowGroupDAO.insertOne(MapperFacadeUtils.map(saveDTO.getM(),WorkFlowGroup.class));
        saveDTO.getM().setId(id);
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg<WorkFlowGroupSaveDTO> editWorkFlowGroup(WorkFlowGroupSaveDTO saveDTO) {
        workFlowGroupDAO.updateOne(MapperFacadeUtils.map(saveDTO.getM(),WorkFlowGroup.class));
        return new ResultMsg<>(ResultConstant.SUCCESS,null,saveDTO);
    }

    @Override
    public ResultMsg deleteWorkFlowGroup(Integer id) {
        workFlowGroupDAO.deleteOne(id);
        return new ResultMsg(ResultConstant.SUCCESS);
    }
}